import CryptoJS from 'crypto-js';
// 非对称加密库
import JSEncrypt from 'jsencrypt';
import { nanoid } from 'nanoid';
import { getCookie } from './getCookie';

// 获取 tooken
export function getToken() {
  let token = localStorage.getItem('token');
  if (getCookie('IPSSESSION') && token !== getCookie('IPSSESSION')) {
    localStorage.setItem('token', getCookie('IPSSESSION'));
    token = localStorage.getItem('token');
  }
  if (!token) {
    token = getCookie('IPSSESSION');
  }
  return token;
}

// 获取随机值
export function getRandomCode(length = 12) {
  if (length > 0) {
    let data = [
      '0',
      '1',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
      'A',
      'B',
      'C',
      'D',
      'E',
      'F',
      'G',
      'H',
      'I',
      'J',
      'K',
      'L',
      'M',
      'N',
      'O',
      'P',
      'Q',
      'R',
      'S',
      'T',
      'U',
      'V',
      'W',
      'X',
      'Y',
      'Z',
      'a',
      'b',
      'c',
      'd',
      'e',
      'f',
      'g',
      'h',
      'i',
      'j',
      'k',
      'l',
      'm',
      'n',
      'o',
      'p',
      'q',
      'r',
      's',
      't',
      'u',
      'v',
      'w',
      'x',
      'y',
      'z'
    ];
    let nums = '';
    for (let i = 0; i < length; i++) {
      let r = parseInt(Math.random() * 61);
      nums += data[r];
    }
    return nums;
  } else {
    return false;
  }
}
// 获取当前页 URL 参数
export function getProps(data) {
  const url = document.location.href,
    tempStr = url;
  let tempArr, tempKV;
  let props = {};
  tempArr = tempStr.split('?');
  tempArr = tempArr[1] ? tempArr[1] : '';
  tempArr = tempArr.split('&');
  if (data) {
    tempArr = data.split('?');
    tempArr = tempArr[1] ? tempArr[1] : '';
    tempArr = tempArr.split('&');
  }
  let tempKey, tempValue;
  for (let i = 0; i < tempArr.length; i++) {
    tempKV = tempArr[i].split('=');
    tempKey = tempKV[0];
    tempKV.splice(0, 1);
    tempValue = tempKV.join('=');
    Object.assign(props, {
      [tempKey]: tempValue
    });
  }
  return props;
}

/**
 * @param 返回的ID长度
 * @returns 返回随机ID
 */
export const getNanoid = len => {
  return nanoid(len);
};

/**
 * 鉴权 规则
 * 有token时：sign=MD5(token + timestamp + nonce + id)
 * 无token时：sign=MD5(timestamp + nonce + id)
 */
export const getAuthentication = () => {
  const token = getToken(),
    timestamp = parseInt(new Date().getTime() / 1000),
    nonce = getRandomCode(),
    id = getNanoid(8);
  let sign = CryptoJS.MD5(token + timestamp + nonce + id).toString();
  if (!token) {
    sign = CryptoJS.MD5(timestamp + nonce + id).toString();
  }
  return {
    token,
    timestamp,
    nonce,
    id,
    sign
  };
};

// https://juejin.cn/post/7066480847351709727
/**
 * 非对称加密
 */
// let aesKey = 'hello world 非对称加密';
// const encrypt = new JSEncrypt.JSEncrypt() // 实例化加密对象
// encrypt.setPublicKey('gongyao') // 设置公钥
// const RSAData = encrypt.encrypt(aesKey) // 要加密的内容
// console.log('加密数据 RSAData', RSAData)
